package com.ykt.validator;

import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.validate.Validator;
import com.ykt.common.ValueConstants;
import com.ykt.common.utils.StringUtil;
import com.ykt.vo.ResultStatus;
import com.ykt.vo.ResultVo;
import com.ykt.exception.SystemException;
import com.ykt.service.RepeatSubmitService;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础校验类
 * @author long.hua
 * @version 1.0.0
 * @since 2015-12-10 00:27
 */
public abstract class BaseValidator extends Validator {

    private RepeatSubmitService repeatSubmitService = Enhancer.enhance(RepeatSubmitService.class.getName(), RepeatSubmitService.class);


    protected abstract void validate(Controller c);

    protected abstract void handleError(Controller c);


    /**封装错误消息*/
    protected Map<String, Object> toMsgMap(Controller c) {
        Map<String, Object> msgMap = new HashMap<>();

        Enumeration<String> enums = c.getAttrNames();
        while (enums.hasMoreElements()) {
            String errorKey = enums.nextElement();
            msgMap.put(errorKey, c.<String>getAttr(errorKey));
        }

        return msgMap;
    }

    /**封装返回值对象*/
    private ResultVo toResultVo(Controller c){
        Map<String, Object> msgMap = toMsgMap(c);

        ResultVo<Map<String, Object>> vo = new ResultVo<>(ResultStatus.PARAM_ERROR, ResultStatus.PARAM_ERROR_MSG);
        vo.setData(msgMap);

        return vo;
    }

    /**错误消息格式化为json格式*/
    protected String toJsonText(Controller c){
        String json = JsonKit.toJson(toResultVo(c));

        // 分页参数为空时移除掉不返回
        if(json.contains(ValueConstants.PAGE_INFO_STRING)){
            json = StringUtil.trimAll(json, ValueConstants.PAGE_INFO_STRING);
        }

        return StringUtil.trimAll(json, '\\');
    }

    protected <T> void validateInSet(int index, T[] values, String errorKey, String errorMessage) {
        Object value = getController().getPara(index);
        validateValueInSet(value, values, errorKey, errorMessage);
    }

    protected <T> void validateInSet(String field, T[] values, String errorKey, String errorMessage) {
        Object value = getController().getPara(field);
        validateValueInSet(value, values, errorKey, errorMessage);
    }

    private <T> void validateValueInSet(Object value, T[] values, String errorKey, String errorMessage) {
        if (value == null) {
            throw new SystemException(ResultStatus.FAILURE, ResultStatus.FAILURE_MSG);
        }

        if (values != null) {
            boolean isIn = false;
            for (T t : values) {
                if (t.toString().equals(value)) {
                    isIn = true;
                    break;
                }
            }
            if (!isIn) {
                addError(errorKey, errorMessage);
            }
        }
    }

    protected void validateRepeatSubmit(int index, String bizKey, String errorKey, String errorMessage){
        Object value = getController().getPara(index);
        validateRepeatSubmitToken(value, bizKey, errorKey, errorMessage);
    }

    protected void validateRepeatSubmit(String field, String bizKey, String errorKey, String errorMessage){
        String token = getController().getPara(field);
        validateRepeatSubmitToken(token, bizKey, errorKey, errorMessage);
    }

    protected void validateRepeatSubmitToken(Object value, String bizKey, String errorKey, String errorMessage) {
        // token为空则不校验
        if (value == null) {
            return;
        }

        // token不为空则校验
        String tokenTmp = repeatSubmitService.getToken(bizKey);
        if(!value.toString().equals(tokenTmp)){
            addError(errorKey, errorMessage);
        }
    }
}
